package cn.com.dhcc.ddi.node.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.node.vo.DataCategory;
import cn.com.dhcc.ddi.node.vo.wapper.DataCategoryRowWapper;
/**
 * 
    * @ClassName: DataCategoryDao
    * @Description: 数据分类DAO
    * @author 18315
    * @date 2017-3-10
    *
 */
@Repository
public class DataCategoryDao extends BaseDao {
	private static Logger logger = Log.getLogger(Log.LOGTYPE.DDI);
	
	/**
	 * 数据分类分页信息列表
	 * 
	 * @return
	 * @throws ServiceException
	 */
	
	public PagerData<DataCategory> list(Pager pager, WhereCondition condition)
			throws DaoException {
		String msg = "";
		String sql = "select t1.*,t2.name domName from "+ SI_TABLE("t_ddi_data_catagory") + " t1 " +
				"  left join "+ SI_TABLE("t_ddi_domain_info") + " t2 on t1.domain_id=t2.id";
							   
		try {
			return CM.getDao().getPagerList("获取数据分页信息列表", sql,
					new DataCategoryRowWapper(), pager, condition, new Object[] {});
		} catch (DaoException e) {
			msg = "获取数据分页信息列表失败";
			logger.error("获取数据分页信息列表失败", e);
			throw new DaoException(msg, e);
		}
	}
	
	/**
	 * 根据业务域id获取数据分类列表
	 * @param  domainId
	 * @return 业务域下数据分类列表
	 * @throws DaoException
	 */
	public List<DataCategory> getDcListByDomainId(String domainId)
			throws DaoException {
		String msg = "";
		String sql = "SELECT * FROM " + SI_TABLE("t_ddi_data_catagory") + " WHERE domain_id = ?";
		try {
			return CM.getDao().list("根据业务域id获取数据分类列表", sql,
					new DataCategoryRowWapper(), new Object[] {domainId});
		} catch (DaoException e) {
			msg = "根据业务域id获取数据分类列表失败";
			throw new DaoException(msg, e);
		}
	}
	
	/**
	 * 根据业务域id获取可用数据分类列表
	 * @param  domainId
	 * @return 业务域下数据分类列表
	 * @throws DaoException
	 */
	public List<DataCategory> getUsableDcListByDomainId(String domainId)
			throws DaoException {
		String msg = "";
		String sql = "SELECT * FROM " + SI_TABLE("t_ddi_data_catagory") + " WHERE domain_id = ? and status = '1' ";
		try {
			return CM.getDao().list("根据业务域id获取可用数据分类列表", sql,
					new DataCategoryRowWapper(), new Object[] {domainId});
		} catch (DaoException e) {
			msg = "根据业务域id获取可用数据分类列表失败";
			throw new DaoException(msg, e);
		}
	}
	
	/**
     * 获取对象
     * @param id
     * @return
     * @throws DaoException
     */
    public DataCategory get(String id) throws DaoException {
        return getDao().getModuleById("获取对象",DataCategory.class,new Object[]{id});
    }
	
	 /**
     * 新增
     * @param obj
     * @return
     * @throws DaoException
     */
    public int insert(DataCategory obj) throws DaoException {
        return getDao().insertModule("新增",obj);
    }
	
    /**
     * 修改
     * @param obj
     * @return
     * @throws DaoException
     */
    public int update(DataCategory obj) throws DaoException {
        return getDao().updateModuleByIdExecuteNull("修改",obj);
    }
    /**
     * 删除 id
     * @param obj
     * @return
     * @throws DaoException
     */
    
    public int delete(String id) throws DaoException {
		return 0;
    	
       
    }
    
    /**
	 * 批量删除(备用)
	    * @Title: batchDelete
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @param ids
	    * @param @return
	    * @param @throws DaoException
	    * @param @throws IllegalArgumentException    参数
	    * @return int    返回类型
	    * @throws
	 */
	
	public int batchDelete(String[] ids) throws DaoException,IllegalArgumentException {
               int num = 0;
			  try {
				 Assert.noNullElements(ids);
				 List<Object[]> params = new ArrayList<Object[]>();
				 for (String id : ids) {
					params.add(new Object[] { id });
				}
				String sql = "update  " + SI_TABLE("t_ddi_node_info")
						+ " set  status='0',mtime=sysdate where id = ?";
				num = CM.getDao().batchUpdate("批量删除", sql, params);
			} catch (DaoException e) {
				throw new DaoException("批量删除失败", e);
		}
			return num;
}
    
    /**
     * 新增或修改
     * @param obj
     * @return
     * @throws DaoException
     */
    public int insertOrUpdate(DataCategory obj) throws DaoException {
        return getDao().saveOrUpdateModuleByIdExecuteNull("新增或修改",obj);
    }
    
    /**
   	 * 验证名称唯一性
   	 * 
   	 * @param name
   	 * @param checkStr
   	 * @return
   	 * @throws DaoException
   	 */
   	
   	public int count(WhereCondition searchParameter) throws DaoException {
   		String msg = "";
   		String sql = "select count(*) from " + SI_TABLE("t_ddi_data_catagory");
   		try {
   			return CM.getDao().queryForInt("数据已存在", sql, searchParameter, new Object[]{});
   		} catch (DaoException e) {
   			msg = "获取数据信息列表失败";
   			logger.error("获取数据信息列表失败", e);
   			throw new DaoException(msg, e);
   		}
   		
   	}

}
